package cn.edu.nju.software.cdiy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Scomment;
import cn.edu.nju.software.cdiy.dao.ScommentDao;

public class ScommentDaoImpl implements ScommentDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.ScommentDao#loadByScommentId(java.lang.Long)
	 * 根据主键去Scomment对象，
	 */
	@Override
	public Scomment getScommentById(Long id) {
		Session session = sessionFactory.getCurrentSession();

		Scomment returnScomment = null;
		try {

			returnScomment = (Scomment) session.load(Scomment.class, id);

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnScomment;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.ScommentDao#loadBySchemeId(java.lang.Long)
	 */
	@Override
	public List<Scomment> getSchemeComments(Long schemeid) {
		Session session = sessionFactory.getCurrentSession();

		List<Scomment> list = null;
		try {

			Query query = session
					.createQuery("from Scomment s where s.schemeid=:schemeid order by s.time desc");
			query.setParameter("schemeid", schemeid);
			list = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.ScommentDao#deleteByScomentId(java.lang.
	 * Long) 根据主键删除Scomment
	 */
	@Override
	public void deleteByScomentId(Long commentid) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
			
			Scomment comment = (Scomment) session.load(Scomment.class,
					commentid);
			session.delete(comment);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.software.cdiy.dao.ScommentDao#saveScomment(cn.edu.nju.software
	 * .cdiy.model.Scomment) 保存Scomment；
	 */
	@Override
	public Scomment saveScomment(Scomment scomment) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
			
			session.save(scomment);

			
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
		return scomment;
	}

	

	@Override
	public int getScommentSize(Long schemeId) {
		Session session = sessionFactory.getCurrentSession();

		int returnInt = 0;
		try {

			Query query = session
					.createQuery("select count(*) from Scomment s where s.schemeid=:schemeid");
			query.setLong("schemeid", schemeId);
			long test = (Long) query.uniqueResult();
			returnInt = (int) test;
		} catch (Exception e) {

			e.printStackTrace();
		} 
		return returnInt;
	}

	@Override
	public List<Scomment> getScommentSelectList(Long schemeId,int page,int pageSize) {
		Session session = sessionFactory.getCurrentSession();

		List<Scomment> returnInt = null;
		try {

			
			Query query = session
					.createQuery("from Scomment s where s.schemeid=:id");
			query.setLong("id",schemeId);
			query.setFirstResult((page - 1) * pageSize);
			query.setMaxResults(pageSize);
			returnInt=query.list();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnInt;
	}

}
